﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using MEsE.NET.Serveripää;

namespace MEsE.NET
{
    /// <summary>
    /// Interaction logic for Login.xaml
    /// </summary>
    public partial class Login : Window
    {
        App controller = App.Current as App;

        public Login()
        {
            InitializeComponent();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            login();
        }

        private void login()
        {
            try
            {
                MEsE_WSClient client = new MEsE_WSClient();
                String username = txtUsername.Text,
                password = txtPassword.Password;


                user User = client.Login(username, password);

                controller.kirjautumaOnnistui(User);
                controller.username = username;
                controller.passu = password;
            }
            catch (Exception ex)
            {
#if DEBUG
                MessageBox.Show("Kirjautuma epäonnistui\n\n" + ex.Message);
#else
                MessageBox.Show("Kirjautuma epäonnistui\n\n" + ex.Message);
                MessageBox.Show("Kirjautuma epäonnistui");
#endif
            }
        }

        private void Window_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Return)
            {
                login();
            }
        }
    }
}
